//
//  RootViewController.m
//  TheCoffeeDrinks
//
//  Created by Ji Hao on 10-7-28.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "RootViewController.h"
#import "FirstLevelViewController.h"

@implementation RootViewController
@synthesize list;
@synthesize window;
@synthesize firstLevelViewController;

- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
	NSMutableArray *tmpArray = [[NSMutableArray alloc] init];
	NSString *path = [[NSBundle mainBundle] pathForResource:@"CoffeeData" ofType:@"plist"];
	NSDictionary * dict = [[NSDictionary alloc] initWithContentsOfFile:path];
	
	for (id key in dict) {
		NSDictionary *coffeeItemDict = [dict objectForKey:key];
		NSString *imageName = [coffeeItemDict objectForKey:@"tableCellImage"];
		NSLog(@"%@", imageName);
		
		[tmpArray addObject:key];
		NSLog(@"size:%d",[tmpArray count]);
		//NSLog(@"key: %@, value: %@", key, [dict objectForKey:key]);
	}
	[dict release];
	self.list = tmpArray;
	NSLog(@"%d",[list count]);
	[tmpArray release];
	
}


/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
 // Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations.
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
 */

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release anything that can be recreated in viewDidLoad or on demand.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [list count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"CoffeeCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    
	// Configure the cell.
	NSString *title = [list objectAtIndex:[indexPath row]];
	
	//Won't be necessary to set the title, since the image already says the meaning
	//cell.textLabel.text = title;
	//cell.textLabel.adjustsFontSizeToFitWidth = TRUE;
	//NSLog([[NSString alloc] initWithFormat:@"%@.png",title]);
	
	cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",title]];
	//we can set something else here
	//cell.detailTextLabel.text = title;
	//cell.detailTextLabel.adjustsFontSizeToFitWidth = TRUE;
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 240.0f;
}


// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // Navigation logic may go here -- for example, create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController animated:YES];
	// [anotherViewController release];
	NSLog(@"Selected %d",[indexPath row]);
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	
	//click and goto detailed page
	self.firstLevelViewController = [[FirstLevelViewController alloc] initWithNibName:@"FirstLevelViewController" bundle:nil];
	NSLog(@"---------Starting details----------------");
	NSString *path = [[NSBundle mainBundle] pathForResource:@"CoffeeData" ofType:@"plist"];
	NSDictionary * dict = [[NSDictionary alloc] initWithContentsOfFile:path];
	NSDictionary * subDict = [dict objectForKey:[list objectAtIndex:[indexPath row]]];
	NSString *desc = [subDict objectForKey:@"description"];
	NSLog(@"-> %@",desc);
	self.firstLevelViewController.text = desc;
	
	//[self.view insertSubview:self.pagingScrollViewController.view atIndex:0];
	[self.navigationController pushViewController:firstLevelViewController animated:YES];
	
	//[tableView addSubview:pagingScrollViewController.view];
    //[tableView makeKeyAndVisible];

}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


- (void)dealloc {
	[list release];
    [super dealloc];
}


@end

